package com.dq.service.goods.impl;

import com.dq.context.Constant;
import com.dq.controller.in.goods.stock.OrgGoodsCentreQuery;
import com.dq.domain.goods.Product;
import com.dq.domain.goods.stock.StockChangeRecord;
import com.dq.exception.customException.AdminException;
import com.dq.service.goods.StockChangeRecordService;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;

import com.dq.domain.goods.centre.OrgGoodsCentre;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.goods.OrgGoodsCentreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author  cf
 */
@Service
public class OrgGoodsCentreServiceImpl extends BaseRepositories implements OrgGoodsCentreService {
    @Autowired
    private StockChangeRecordService stockChangeRecordService;

    /**
     * 对象初始化
     */
    @Override
    public OrgGoodsCentre initObject(OrgGoodsCentre orgGoodsCentre) throws AdminException {
        SysUser user = sysUserService.getUser();
        orgGoodsCentre.setCreateUser(user);
        orgGoodsCentre.setUpdateUser(user);

        return orgGoodsCentre;
    }
    /**
     * 删除商品门店关联对象, 生生库存变动记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delList(List<OrgGoodsCentre> list) throws AdminException {
        SysUser loginUser = sysUserService.getUserInSection();

        for (OrgGoodsCentre orgGoodsCentre : list) {
            orgGoodsCentre.setDeleted(true);
            orgGoodsCentre.setUpdateUser(loginUser);

            // 添加 库存变动, 细化到product
            StockChangeRecord stockChangeRecord = new StockChangeRecord(StockChangeRecord.ChangeType.loss, StockChangeRecord.ChangeDirection.reduce, orgGoodsCentre.getCount());
            stockChangeRecord = stockChangeRecordService.saveStockChangeRecord(stockChangeRecord, orgGoodsCentre.getGoods(), orgGoodsCentre.getProduct(), orgGoodsCentre.getOrg(), false);

        }
        orgGoodsCentreRepository.saveAll(list);

    }

}
